package IOTWeb.com.utils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import IOTWeb.com.IOTMain;

import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;


//定时器
public class DayInterval implements ServletContextListener {




    public void showDayTime() {

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.set(year, month, day, 23, 59, 59);//设置要执行的日期时间

        Date defaultdate = calendar.getTime();

        Timer dTimer = new Timer();
        dTimer.schedule(new TimerTask() {



	    @Override
	    public void run() {
	        LocalDate today = LocalDate.now();
	    	LogSet.logger.info("更新tocken！！！");
			Set<LocalDate> keySet = IOTMain.token.keySet();
			Iterator<LocalDate> it =keySet.iterator();
			while(it.hasNext()){
				LocalDate key = it.next();
			    String value = IOTMain.token.get(key);
			    Period p = Period.between(key, today);
			    if(p.getDays()>1) {
			    	IOTMain.token.remove(key,value);
			    	String value1 = IOTMain.devicelist.get(value);
			    	if(value1!=null)
			    		IOTMain.devicelist.remove(value,value1);
			    }
			}
	        }
        }, defaultdate , 24* 60* 60* 1000);//24* 60* 60 * 1000
    }

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

}
